import java.util.concurrent.atomic.AtomicInteger;

/**
 * @author zhangdi
 * @version 1.0
 * @date 2021.11.29 14:22
 */
public class ThreadLocalDemo {
    ThreadLocal<Integer> val = new ThreadLocal<>();

//    volatile int val2 = 0;

    AtomicInteger val3 = new AtomicInteger(0);

    public void add() {
//        val3.incrementAndGet();
//        ++val2;
//        if (val.get() == null) {
//            val.set(1);
//        } else {
//            val.set(val.get() + 1);
//        }
//        print("t-");
    }

    public void print(String flag) {
        System.out.println(flag + val3.get());
    }

    public void clean() {
        val.remove();
    }

    public static void main(String[] args) {
        ThreadLocalDemo tld = new ThreadLocalDemo();

        for (int i = 0; i < 20; i++) {
            int n = i;
            Thread tt = new Thread(() -> {
                tld.print("t" + n + "- ");
                tld.add();
            });
            tt.start();
        }
    }
}
